<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 18-1-5
 * Time: 下午3:29
 */

namespace app\common;



use think\Exception;

class bjphbj
{
//    private static $url = "https://oli.bjphbj.com:8080/";
//    private static $sn = 'a87ff679a2f3e71d9181a67b7542122c';
//    private static $key = 'VabltC!DQ(]-&E1ynwH+P$g+UqrR/R<Qd&Ai8(HZKF@/A2m93V=Su2YRV==I$JN6R.yu^6xu+s]C8o3G[}*gyXb]J[YEji$&!(,NF8qC}Nq!XigJ1U>jE}gA3^U,#t0J';

//正式
    private static $url = "https://oli.bjphbj.com/";
    private static $sn = '6364d3f0f495b6ab9dcf8d3b5c6e0b01';
    private static $key = 'KCHM@F*d/a#u0<QL@P7:PXXqO-v+$V(LUw7EHW8w]9(B/joG*<91a&2OHo0bze/_y+:.dy3ay(|q5ZR<3>J.c,j|h<cJ-rE5qX_DYN]jk(oZwP+@cs*L:sNi@+z3SjFu';
    private static $notifyURL = "http://121.40.215.32:8080/callback.php";
    static function get_goods()
    {
        $info = ["error"=>1,"error_info"=>"","data"=>[]];
        $result = self::get_curl("/open/goods","");
        if($result["code"] == 200)
        {
            $info["error"] = 0;
            $info["data"] = json_decode($result["body"],true);
        }else{
            $info["error_info"] = $result["body"];
        }
        return $info;
    }
    static function get_info()
    {
        $info = ["error"=>1,"error_info"=>"","data"=>[]];
        $result = self::get_curl("/open/info","");
        if($result["code"] == 200)
        {
            $info["error"] = 0;
            $info["data"] = json_decode($result["body"],true);
        }else{
            $info["error_info"] = $result["body"];
        }
        return $info;
    }
    static function query($order_no)
    {
        $info = ["error"=>1,"error_info"=>"","data"=>[]];
        $result = self::get_curl("/open/query",json_encode(["tradeNO"=>$order_no]));
        if($result["code"] == 200)
        {
            $info["error"] = 0;
            $info["data"] = json_decode($result["body"],true);
        }else{
            $info["error_info"] = $result["body"];
        }
        return $info;
    }
    static function recharge($goods_id,$card_no,$order_no)
    {
        $info = ["error"=>1,"error_info"=>"","data"=>[]];
        $result = self::get_curl("/open/recharge",
            json_encode([
                "goodsID"=>$goods_id,
                "cardNO"=>$card_no,
                "tradeNO"=>$order_no,
                "notifyURL"=>self::$notifyURL
            ]));
        if($result["code"] == 202)
        {
            $info["error"] = 0;
        }else{
            $info["error_info"] = $result["body"];
        }
        return $info;
    }
    static function callback()
    {
        $info = ["error"=>1,"error_info"=>"","data"=>[]];
        $raw_post_data = file_get_contents('php://input', 'r');
        try{
            $raw_post_data = json_decode($raw_post_data,true);
            if(empty($raw_post_data))
                throw new Exception("数据为空");
            $order_no = $raw_post_data["tradeNO"];
            if(empty($order_no))
                throw new Exception("没有订单号");
            $query_result = self::query($order_no);
            if(empty($query_result) ||$query_result["error"] == 1)
                throw new Exception("查询信息失败");
            $state = $query_result["data"]["state"];
            $info["data"]["order_no"] = $order_no;
            if(in_array($state,["notifying","complete"]))
            {
                $info["data"]["status"] = 2;
            }elseif(in_array($state,["refund"]))
            {
                $info["data"]["status"] = 3;
            }else{
                $info["data"]["status"] = 1;
            }
            $info["error"] = 0;
        }catch (\Exception $e)
        {

        }
        return $info;
    }



    static function get_curl($action,$body)
    {
        $headers = ["Authorization: ".self::$sn." ".md5($body.self::$key),'Content-Type: application/json'];
        $url = self::$url.$action;
        return self::curl_url($url,$headers,$body);
    }
    static function curl_url($url,$headers,$body)
    {
        $result = ["code"=>500,"body"=>"","error_info"=>""];
        try{
            $ch = curl_init();
            //设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            if(!empty($headers))
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            if($body)
            {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            }
            //执行并获取HTML文档内容
            $output = curl_exec($ch);
            if($output === false){
                $result["error_info"]= curl_errno($ch);
            }
            $result["code"] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
            $result["body"] = $output;
            //释放curl句柄
            curl_close($ch);
        }catch (\Exception $e)
        {
            $result["error_info"] = $e->getMessage();
        }
        return $result;
    }


}